package com.lin.androidgame21;

/***

 *Create By LWL Write

 **/

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.MotionEvent;

import java.io.IOException;
import java.util.Random;

public class Combat {

    private Context context;
    private float scaleX;
    private float scaleY;
    private Sprite sprite_player1;
    private Sprite sprite_player2;
    private Sprite sprite_enemy;
    private Sprite sprite_enemy_attack;
    private Sprite sprite_fire;
    private Sprite sprite_ice;
    private int step;
    private ComBatMenuState comBatMenuState;
    private RectF rectF;
    private Sprite sprite_fall_down1;
    private Sprite sprite_fall_down2;
    private int playerTotalHP1;
    private int playerCurrentHP1;
    private int playerTotalMP1;
    private int playerCurrentMP1;
    private int playerTotalHP2;
    private int playerCurrentHP2;
    private int playerTotalMP2;
    private int playerCurrentMP2;
    private int enemyTotalHP;
    private int enemyCurrentHP;
    private int enemyTotalMP;
    private int enemyCurrentMP;
    private Random random;
    private boolean isHitPlayer1;
    private boolean isHitPlayer2;
    private boolean isHitEnemy1;
    private boolean isHitEnemy2;
    private String playerHurtString1;
    private String playerHurtString2;
    private String enemyHurtString;
    private int delay0;
    private int delay1;
    private int delay2;
    private int delay3;
    private int delay4;
    private ResultListener resultListener;
    private GameState result ;
    private MySoundPool mySoundPool;

    interface ResultListener{
        void result();
    }

    public Combat(Context context, float scaleX, float scaleY) {
        this.context = context;
        this.scaleX = scaleX;
        this.scaleY = scaleY;
        init();
    }

    private void init() {
        mySoundPool=new MySoundPool(context);
        sprite_enemy = new Sprite(getBitmap("enemy.png"), 150, 170);
        sprite_enemy.setPosition(150, 100);
        sprite_enemy.setVisible(true);
        sprite_player1 = new Sprite(getBitmap("combat_player1.png"));
        sprite_player1.setPosition(900, 550);
        sprite_player1.setVisible(true);
        sprite_player2 = new Sprite(getBitmap("combat_player2.png"));
        sprite_player2.setPosition(1000, 500);
        sprite_player2.setVisible(true);
        sprite_enemy_attack = new Sprite(getBitmap("enemy_attack.png"), 170, 170);
        sprite_enemy_attack.setPosition(870, 450);
        sprite_fire = new Sprite(getBitmap("fire.png"), 186, 186);
        sprite_fire.setPosition(150, 100);
        sprite_ice = new Sprite(getBitmap("ice.png"), 144, 245);
        sprite_ice.setPosition(150, 100);
        sprite_fall_down1 = new Sprite(getBitmap("fall_down1.png"));
        sprite_fall_down1.setPosition(900, 550);
        sprite_fall_down2 = new Sprite(getBitmap("fall_down2.png"));
        sprite_fall_down2.setPosition(1000, 500);
        step = 0;
        rectF = new RectF();
        comBatMenuState = ComBatMenuState.NULL;
        random = new Random();
    }

    private Bitmap getBitmap(String path) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(context.getAssets().open(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public void logic() {
        step++;
        if (sprite_enemy.isVisible()) {
            sprite_enemy.nextFrame();
        }
        if ((playerCurrentHP1 > 0 || playerCurrentHP2 > 0) && enemyCurrentHP > 0 && step % 100 == 0) {
            sprite_enemy.setVisible(false);
            sprite_enemy_attack.setVisible(true);
            mySoundPool.play(mySoundPool.getSound_enemy_gong());
        }
        if (step % 3 == 0) {
            if (sprite_enemy_attack.isVisible()) {
                sprite_enemy_attack.nextFrame();
                if (sprite_enemy_attack.getFrameSequenceIndex() == 0) {
                    sprite_enemy_attack.setVisible(false);
                    sprite_enemy.setVisible(true);
                    playerHurtCompute1();
                    playerHurtCompute2();
                }
            }
            if (sprite_fire.isVisible()) {
                sprite_fire.nextFrame();
                if (sprite_fire.getFrameSequenceIndex() == 0) {
                    sprite_fire.setVisible(false);
                    enemyHurtCompute1();
                }
            }
            if (sprite_ice.isVisible()) {
                sprite_ice.nextFrame();
                if (sprite_ice.getFrameSequenceIndex() == 0) {
                    sprite_ice.setVisible(false);
                    enemyHurtCompute2();
                }
            }
            collision();
            isDead();
        }
    }

    public void isDead() {
        if (playerCurrentHP1 == 0) {
            sprite_player1.setVisible(false);
            sprite_fall_down1.setVisible(true);
        }
        if (playerCurrentHP2 == 0) {
            sprite_player2.setVisible(false);
            sprite_fall_down2.setVisible(true);
        }
        if (enemyCurrentHP == 0) {
            sprite_enemy.setVisible(false);
            mySoundPool.play(mySoundPool.getSound_enemy_die());
            if (delay3++==5){
                delay3=0;
                setResult(GameState.WIN);
                if (resultListener!=null) {
                    resultListener.result();
                }
            }
        }
        if (playerCurrentHP1==0&&playerCurrentHP2==0){
            if (delay4++==5) {
                delay4 = 0;
                setResult(GameState.OVER);
                if (resultListener != null){
                    resultListener.result();
                }
            }
        }
    }

    private void collision() {
        if (!isHitPlayer1) {
            if (sprite_enemy_attack.collisionWith(sprite_player1)) {
                isHitPlayer1 = true;
            }
        }
        if (!isHitPlayer2) {
            if (sprite_enemy_attack.collisionWith(sprite_player2)) {
                isHitPlayer2 = true;
            }
        }
        if (!isHitEnemy1) {
            if (sprite_fire.collisionWith(sprite_enemy)) {
                isHitEnemy1 = true;
            }
        }
        if (!isHitEnemy2) {
            if (sprite_ice.collisionWith(sprite_enemy)) {
                isHitEnemy2 = true;
            }
        }

    }

    public void playerHurtCompute1() {
        if (isHitPlayer1) {
            if (random.nextInt() % 10 == 0) {
                playerHurtString1 = "miss";
            } else {
                int hurt = 10 + random.nextInt(5);
                playerHurtString1 = "-" + hurt;
                playerCurrentHP1 -= hurt;
                mySoundPool.play(mySoundPool.getSound_zhanglingershang());
                if (playerCurrentHP1 < 0)
                    playerCurrentHP1 = 0;
            }
            isHitPlayer1 = false;
        }
    }

    public void playerHurtCompute2() {
        if (isHitPlayer2) {
            if (random.nextInt() % 10 == 0) {
                playerHurtString2 = "miss";
            } else {
                int hurt = 10 + random.nextInt(5);
                playerHurtString2 = "-" + hurt;
                playerCurrentHP2 -= hurt;
                mySoundPool.play(mySoundPool.getSound_lixiaoyaoshang());
                if (playerCurrentHP2 < 0)
                    playerCurrentHP2 = 0;
            }
            isHitPlayer1 = false;
        }
    }

    public void enemyHurtCompute1() {
        if (isHitEnemy1) {
            if (random.nextInt() % 10 == 0)
                enemyHurtString = "miss";
            else {
                int hurt = 20 + random.nextInt(10);
                enemyHurtString = "-" + hurt;
                enemyCurrentHP -= hurt;
                if (enemyCurrentHP < 0)
                    enemyCurrentHP = 0;
            }
            isHitEnemy1 = false;
        }
    }

    public void enemyHurtCompute2() {
        if (isHitEnemy2) {
            if (random.nextInt() % 10 == 0) {
                enemyHurtString = "miss";
            } else {
                int hurt = 10 + random.nextInt(10);
                enemyHurtString = "-" + hurt;
                enemyCurrentHP -= hurt;
                if (enemyCurrentHP < 0) {
                    enemyCurrentHP = 0;
                }
            }
            isHitEnemy2 = false;
        }
    }


    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(getBitmap("combat1.png"), 0, 0, null);
        sprite_enemy.draw(canvas);
        sprite_fire.draw(canvas);
        sprite_ice.draw(canvas);
        sprite_enemy_attack.draw(canvas);
        sprite_player1.draw(canvas);
        sprite_player2.draw(canvas);
        sprite_fall_down1.draw(canvas);
        sprite_fall_down2.draw(canvas);
        canvas.drawBitmap(getBitmap("gong.png"), 1000, 350, null);
        canvas.drawBitmap(getBitmap("fang.png"), 1000, 150, null);
        canvas.drawBitmap(getBitmap("wu.png"), 1100, 250, null);
        canvas.drawBitmap(getBitmap("shu.png"), 900, 250, null);
        switch (comBatMenuState) {
            case SHU:
                canvas.drawBitmap(getBitmap("choice.png"), 500, 150, null);
                paint.setTextSize(30);
                if (!sprite_fire.isVisible()&&playerCurrentHP1 > 0 && playerCurrentMP1 >= 10) {
                    paint.setColor(Color.BLUE);
                } else {
                    paint.setColor(Color.GRAY);
                }
                canvas.drawText("三昧真火        气10", 540, 240, paint);
                if (!sprite_ice.isVisible()&&playerCurrentHP2 > 0 && playerCurrentMP2 >= 10) {
                    paint.setColor(Color.BLUE);
                } else {
                    paint.setColor(Color.GRAY);
                }
                canvas.drawText("风雪冰天        气10", 540, 280, paint);
                break;
        }
        canvas.drawBitmap(getBitmap("zhuangtai1.png"), 100, 625, null);
        canvas.drawBitmap(getBitmap("zhuangtai2.png"), 300, 625, null);

        paint.setColor(Color.RED);
        paint.setTextSize(14);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextAlign(Paint.Align.RIGHT);
        canvas.drawText(playerCurrentHP1 + "/" + playerTotalHP1, 230, 700, paint);
        canvas.drawText(playerCurrentHP2 + "/" + playerTotalHP2, 430, 700, paint);
        paint.setColor(Color.BLUE);
        canvas.drawText(playerCurrentMP1 + "/" + playerTotalMP1, 230, 720, paint);
        canvas.drawText(playerCurrentMP2 + "/" + playerTotalMP2, 420, 720, paint);
        paint.setColor(Color.RED);
        paint.setTextSize(24);
        if (enemyHurtString != null) {
            canvas.drawText(enemyHurtString, 250, 80, paint);
            if (delay0++ == 5) {
                delay0 = 0;
                enemyHurtString = null;
            }
        }
        if (playerHurtString1 != null) {
            canvas.drawText(playerHurtString1, 950, 530, paint);
            if (delay1++ == 5) {
                delay1 = 0;
                playerHurtString1 = null;
            }
        }
        if (playerHurtString2 != null) {
            canvas.drawText(playerHurtString2, 1050, 480, paint);
            if (delay2++ == 5) {
                delay2 = 0;
                playerHurtString2 = null;
            }
        }
    }

    public void singleTap(MotionEvent e) {
        switch (comBatMenuState) {
            case NULL:
                rectF.set(900 * scaleX, 250 * scaleY, 980 * scaleX, 330 * scaleY);
                if (rectF.contains(e.getX(), e.getY())) {
                    comBatMenuState = ComBatMenuState.SHU;
                }
                break;
            case SHU:
                rectF.set(900 * scaleX, 250 * scaleY, 980 * scaleX, 330 * scaleY);
                if (rectF.contains(e.getX(), e.getY())) {
                    comBatMenuState = ComBatMenuState.NULL;
                }
                rectF.set(540 * scaleX, 210 * scaleY, 780 * scaleX, 240 * scaleY);
                if (rectF.contains(e.getX(), e.getY())) {
                    if (!sprite_fire.isVisible()&&playerCurrentHP1 > 0 && playerCurrentMP1 >= 10) {
                        playerCurrentMP1 -= 10;
                        sprite_fire.setVisible(true);
                        mySoundPool.play(mySoundPool.getSound_fire());
                        comBatMenuState = ComBatMenuState.NULL;
                    }
                }
                rectF.set(540 * scaleX, 250 * scaleY, 780 * scaleX, 280 * scaleY);
                if (!sprite_ice.isVisible()&&rectF.contains(e.getX(), e.getY())) {
                    if (!sprite_ice.isVisible()&&playerCurrentHP2 > 0 && playerCurrentMP2 >= 10) {
                        playerCurrentMP2 -= 10;
                        sprite_ice.setVisible(true);
                        mySoundPool.play(mySoundPool.getSound_ice());
                        comBatMenuState = ComBatMenuState.NULL;
                    }
                }
                break;
        }
    }



    public void setPlayerState1(int playerTotalHP1, int playerCurrentHP1, int playerTotalMP1, int playerCurrentMP1) {
        this.playerTotalHP1 = playerTotalHP1;
        this.playerCurrentHP1 = playerCurrentHP1;
        this.playerTotalMP1 = playerTotalMP1;
        this.playerCurrentMP1 = playerCurrentMP1;

    }

    public void setPlayerState2(int playerTotalHP2, int playerCurrentHP2, int playerTotalMP2, int playerCurrentMP2) {
        this.playerTotalHP2 = playerTotalHP2;
        this.playerCurrentHP2 = playerCurrentHP2;
        this.playerTotalMP2 = playerTotalMP2;
        this.playerCurrentMP2 = playerCurrentMP2;

    }

    public void setEnemyState(int enemyTotalHP, int enemyCurrentHP, int enemyTotalMP, int enemyCurrentMP) {
        this.enemyTotalHP = enemyTotalHP;
        this.enemyCurrentHP = enemyCurrentHP;
        this.enemyTotalMP = enemyTotalMP;
        this.enemyCurrentMP = enemyCurrentMP;
    }

    public void setResultListener(ResultListener resultListener) {
        this.resultListener = resultListener;
    }

    public void setResult(GameState result) {
        this.result = result;
    }

    public GameState getResult() {
        return result;
    }
}